#!/bin/bash
apt install -y --no-install-recommends dbus dbus-x11 cups pulseaudio
mkdir -p /var/run/dbus
arch=$(dpkg --print-architecture)
if [[ $arch == 'amd64' ]]; then
    curl -fkSL "https://www.nomachine.com/free/linux/64/deb" -o nomachine.deb
elif  [[ $arch == 'arm64' ]]; then
    curl -fkSL "https://www.nomachine.com/free/arm/64/deb" -o nomachine.deb
else
    echo "unsupported architecture: $arch"
    exit -1
fi
dpkg -i nomachine.deb
rm nomachine.deb

sed -i -e "s|#EnableClipboard both|EnableClipboard both|g" \
-e "/#DisplayGeometry/c\DisplayGeometry ${SIZEW}x${SIZEH}" /usr/NX/etc/server.cfg

sed -i '/DefaultDesktopCommand/c\DefaultDesktopCommand "xset s off && /usr/bin/startxfce4"' /usr/NX/etc/node.cfg &&\
# config pulseaudio
cp -R /etc/pulse/* /usr/NX/share/audio/ &&\
sed -i -e'/AudioInterface pipewire/c\AudioInterface pulseaudio' \
-e '/CommandStartPulseAudio/c\CommandStartPulseAudio "/usr/bin/pulseaudio --high-priority=no"' \
-e "/#DisplayServerVideoFrameRate/c\DisplayServerVideoFrameRate ${FPS}" \
-e "/#DefaultXDPI/c\DefaultXDPI ${DPI}" /usr/NX/etc/node.cfg
# add VirtualGL to PATH
echo -e "export PATH=\$PATH:/usr/NX/scripts/vgl\n\
export VGL_DISPLAY=$VGL_DISPLAY" > /etc/profile.d/vgl.sh